package uno.linze.interview_question.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        // 配置 URL 路径的权限
        http.authorizeHttpRequests(authorize -> authorize
                                // 允许所有人访问 /public/** 路径下的所有接口
                                .requestMatchers("/**").permitAll()
//                        // 允许所有人访问 /api/guest 接口
//                        .requestMatchers("/api/guest").permitAll()
                                // 其他所有请求都需要认证
                                .anyRequest().authenticated()
                );

        // 应用不需要 CSRF 保护，可以禁用它
        http.csrf(csrf -> csrf.disable());

        return http.build();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        // 定义一个 BCryptPasswordEncoder Bean
        return new BCryptPasswordEncoder();
    }

}
